package com.feiyu.common.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.feiyu.common.util.WrapperBuilder;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tang ming
 * @since 2022/1/3 20:14
 */
public class BaseController<S extends IService<E>, E> {
    @Autowired
    protected S service;

    @GetMapping("/get")
    public R get(E entity){
        return R.data(service.getOne(Condition.getQueryWrapper(entity)));
    }

    @GetMapping("/list")
    public R list(E entity){
        return R.data(service.list(Condition.getQueryWrapper(entity)));
    }

    @PostMapping("/page")
    public R<IPage<E>> list(@RequestBody Map<String, Object> map) {
        IPage<E> pages = service.page(WrapperBuilder.getPage(map), WrapperBuilder.getQuery(map));
        return R.data(pages);
    }

    @PostMapping("/save")
    public R save(@RequestBody E entity){
        return R.status(service.save(entity));
    }

    @PostMapping("/update")
    public R update(@RequestBody E entity){
        return R.status(service.updateById(entity));
    }

    @PostMapping("/delete")
    public R delete(@RequestBody ArrayList<String> ids){
        return R.status(service.removeByIds(ids));
    }
}
